package C3P0;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo1 {
    //创建连接池对象
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    @Test
    public void findAll() throws SQLException {
        //通过连接池获取连接对象
        Connection con = dataSource.getConnection();
        //创建可执行对象
        Statement st = con.createStatement();
        //执行并获取结果
        String sql = "select * from user";
        ResultSet rs = st.executeQuery(sql);

        while (rs.next()) {
            //获取当前行数据
            int uid = rs.getInt("uid");
            String name = rs.getString("name");
            String sex = rs.getString("sex");
            double money = rs.getDouble("money");
            String address = rs.getString("address");

            System.out.println(uid + "\t" + name + "\t" + sex + "\t" + money + "\t" + address);
        }
        //释放资源
        rs.close();
        st.close();
        //如果从连接池中获取的连接 调用close()方法会将此连接归还到连接池
        con.close();
    }
}
